package com.example.foo.admin.web.account;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.web.bind.annotation.GetMapping;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtils;
import com.example.core.PageResult;
import com.example.core.Pageable;
import com.example.foo.account.model.AccountItem;
import com.example.foo.account.query.AccountItemQuery;
import com.example.foo.account.service.AccountService;
import com.example.foo.ftt.model.FttUser;
import com.example.foo.ftt.service.FttUserService;

@org.springframework.web.bind.annotation.RestController
public class AccountItemAction {

	private final AccountService accountService;
	private final FttUserService fttUserService;

	public AccountItemAction(AccountService accountService,
			FttUserService fttUserService) {
		this.accountService = accountService;
		this.fttUserService = fttUserService;
	}

	@GetMapping("/api/accountitems")
	public AjaxResult index(AccountItemQuery accountItemQuery, Pageable pageable) {
		PageResult<AccountItem> pageResult = accountService.findAccountItems(accountItemQuery, pageable);

		List<Long> userIds = pageResult.getRows().stream().map(AccountItem::getUserId).collect(Collectors.toList());
		Map<Long, FttUser> userIdFttUserMap = fttUserService.findFttUsersByUserIdsAsMap(userIds);

		List<AccountItemVO> accountItemVOs = pageResult.getRows()
				.stream()
				.map(accountItem -> new AccountItemVO(accountItem, userIdFttUserMap.get(accountItem.getUserId())))
				.collect(Collectors.toList());;
		return AjaxResult.successData(new PageResult<>(accountItemVOs, pageResult.getTotal()));
	}

	@GetMapping("/api/accountitems/export")
	public AjaxResult export(AccountItemQuery accountItemQuery) {
		Pageable pageable = new Pageable(1, 65535);
		PageResult<AccountItem> pageResult = accountService.findAccountItems(accountItemQuery, pageable);

		List<Long> userIds = pageResult.getRows().stream().map(AccountItem::getUserId).collect(Collectors.toList());
		Map<Long, FttUser> userIdFttUserMap = fttUserService.findFttUsersByUserIdsAsMap(userIds);

		List<AccountItemVO> accountItemVOs = pageResult.getRows()
				.stream()
				.map(accountItem -> new AccountItemVO(accountItem, userIdFttUserMap.get(accountItem.getUserId())))
				.collect(Collectors.toList());;

		Map<String, String> fields = new LinkedHashMap<>();
		fields.put("ID", "id");
		fields.put("创建时间", "createdAt");
		fields.put("科目", "acctc");
		fields.put("科目T", "acctcText");
		fields.put("符号", "drcr");
		fields.put("符号T", "drcrText");
		fields.put("名称", "name");
		fields.put("交易额", "value");
		fields.put("userId", "userId");
		fields.put("对方用户ID", "anotherUserId");
		fields.put("关联类型", "relType");
		fields.put("关联目标", "relId");
		fields.put("账期月份", "periodMonth");
		fields.put("账期日期", "periodDay");
		fields.put("角色", "roleLevel");
		fields.put("角色T", "roleLevelText");
		fields.put("网体级别", "fttLevel");
		fields.put("网体级别T", "fttLevelText");
		fields.put("手机号", "cellphone");
		fields.put("姓名", "idname");
		return new ExcelUtils<>(AccountItemVO.class).export(fields, accountItemVOs);
	}

}
